/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.tests.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Random;

import net.jeremybrooks.knicker.WordsApi;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class RandomTextGen {

	public static final String PARAGRAPH = "p";
	
	static final ArrayList<String> nouns = new ArrayList<String>();
	
	static Random random = new Random();
	
	static{
		try {
			
			InputStream is = RandomTextGen.class.getResourceAsStream("Nouns.txt");
			if(is==null)System.out.println("is was null .... ");
			
			BufferedReader fh = new BufferedReader(new InputStreamReader( is , "UTF8"));
			    String s;
			    int i = 0;
			    while ((s=fh.readLine())!=null){
			    	nouns.add(s);
			    }
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("NOUNS INITIALIZED : " + nouns.toString());
	}
	
	
	
	public static String getRandomGiberish() throws Exception{
		return getRandomGiberish(PARAGRAPH, 1, 30, 50);
	}

	public static String getRandomGiberish(String elementType,
			int numberOfElems, int minWords, int maxWords) throws Exception {
		String response = "";
		String urlStr = "http://www.randomtext.me/api/gibberish/" + elementType
				+ "-" + numberOfElems + "/" + minWords + "-" + maxWords + "/";
		URL oracle = new URL(urlStr);
		URLConnection conn = oracle.openConnection();
		// fake request coming from browser
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
		// retrieve result
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), "UTF-8"));
		StringBuilder sb = new StringBuilder();
		String line;
		while ((line = br.readLine()) != null) {
			sb.append(line);
			sb.append("\n");
		}
		br.close();
		response = sb.toString();
		JSONObject obj = (JSONObject) JSONValue.parse(response);
		String tout = (String) obj.get("text_out");
		return tout;
	}
	
	
	public static String getRandomWordnik() throws Exception{
		String key = "6f0c1a8b19b50f65ce0000fd18700efb0051b7f7b932bb7cc";
		System.setProperty("WORDNIK_API_KEY", key);
		String w = WordsApi.randomWord().getWord();
		return w;
	}
	
	 
	
	
	public static String getRandomWord() throws Exception{
		return nouns.get(random.nextInt(nouns.size()-1));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args){
		try {
			System.out.println(getRandomGiberish());
			System.out.println(getRandomWord());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
